//app.js
App({
  onLaunch: function () {
    var that = this
    wx.getSetting({
      success: res => {
        // console.log(res)
        if (res.authSetting['scope.userInfo']) {
          that.globalData.isAuthorized =  true
          wx.getUserInfo({
            success: res => {
              // console.log(res)
              that.globalData.userInfo = res.userInfo
              that.globalData.res = res
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
            }
          })
        }else{
          that.globalData.isAuthorized =  false
        }
        if (res.authSetting['scope.werun'] && res.authSetting['scope.werun'] == true) {
          that.globalData.isWeRun =  true
        }else{
          that.globalData.isWeRun =  false
        }
      }
    })

    wx.login({
        success:function(login){
          // console.log(login.code)
          // 发送请求
          wx.request({
              url: that.globalData.apiUrl +'/get_openid', //接口地址
              data: {
                code: login.code,
              },
              method: 'get',
            header: {
                'content-type': 'application/json' //默认值
            },
            success: function (res) {
              console.log(res)
              //判断用户是否存在
              if(res.data.code == 1){
                //后台有数据
                that.globalData.userId = res.data.data.id;
                // console.log(app.globalData.userId);
                if (that.userIdCallback) {
                  that.userIdCallback(that.globalData.userId)
                }
              }else if(res.data.code == 2){
                wx.login({
                  success:function(login){
                    // console.log(login.code)
                    // 发送请求
                    wx.request({
                        url: that.globalData.apiUrl +'/get_openid', //接口地址
                        data: {
                          code: login.code,
                        },
                        method: 'get',
                      header: {
                          'content-type': 'application/json' //默认值
                      },
                      success: function (res) {
                        console.log(res)
                        //判断用户是否存在
                        if(res.data.code == 1){
                          //后台有数据
                          that.globalData.userId = res.data.data.id;
                          // console.log(app.globalData.userId);
                          if (that.userIdCallback) {
                            that.userIdCallback(that.globalData.userId)
                          }
                        }else if(res.data.code == 0){
                          that.globalData.openid = res.data.openid
                        }else if(res.data.code == 2){
                          console.log('123213212321')
                        }
                      }
                    })
                  }
                })
              }else if(res.data.code == 0){
                that.globalData.openid = res.data.openid
              }
            }
          })
        }
      })
  },
  
  globalData: {
    userInfo: null,
    isWeRun: '',
    userId: '',
    isOne: false,
    apiUrl:'https://tzgl.tfnhg.com',
    openid:'',
    puid: '',
    code : null,
    res: null,
    themeColor: '#50d2c2'
  }
})